package cn.edu.xcu.ckl;

import android.util.Log;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import cn.edu.xcu.ckl.base.BaseActivity;
import cn.edu.xcu.ckl.fragment.CategoryFragment;
import cn.edu.xcu.ckl.fragment.HomeFragment;
import cn.edu.xcu.ckl.fragment.MineFragment;
import cn.edu.xcu.ckl.fragment.ShoppingCartFragment;
import cn.edu.xcu.ckl.utils.AcListener;


/**
 * 主页
 */
public class MainActivity extends BaseActivity implements AcListener {
    //定义4个fragment
    private HomeFragment mHomeFragment;
    private CategoryFragment mCategoryFragment;
    private ShoppingCartFragment mShoppingCartFragment;
    private MineFragment mMineFragment;

    private BottomNavigationView mBottomNavigationView;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        //初始化控件
        mBottomNavigationView = findViewById(R.id.bottomNavigationView);
    }

    @Override
    protected void setListener() {
        //mBottomNavigationView设置点击事件
        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item.getItemId() == R.id.mr_home) {
                    selectedFragment(0);
                } else if (item.getItemId() == R.id.mr_category) {
                    selectedFragment(1);
                } else if (item.getItemId() == R.id.mr_car) {
                    selectedFragment(2);
                } else {
                    selectedFragment(3);
                }
                return true;
            }
        });
    }

    @Override
    protected void initData() {
        //默认首页选中
        selectedFragment(0);
    }

    private void selectedFragment(int position) {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);

        if (position == 0) {
            if (mHomeFragment == null) {
                mHomeFragment = new HomeFragment();
                fragmentTransaction.add(R.id.content, mHomeFragment);
            } else {
                fragmentTransaction.show(mHomeFragment);
            }
        } else if (position == 1) {
            if (mCategoryFragment == null) {
                mCategoryFragment = new CategoryFragment();
                fragmentTransaction.add(R.id.content, mCategoryFragment);
            } else {
                fragmentTransaction.show(mCategoryFragment);
                mCategoryFragment.loadData();


            }
        } else if (position == 2) {
            if (mShoppingCartFragment == null) {
                mShoppingCartFragment = new ShoppingCartFragment();
                fragmentTransaction.add(R.id.content, mShoppingCartFragment);
            } else {
                fragmentTransaction.show(mShoppingCartFragment);
                mShoppingCartFragment.loadData();
            }
        } else {
            if (mMineFragment == null) {
                mMineFragment = new MineFragment();
                fragmentTransaction.add(R.id.content, mMineFragment);
            } else {
                fragmentTransaction.show(mMineFragment);
            }
        }


        //一定要提交
        fragmentTransaction.commit();


    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {

        if (mHomeFragment != null) {
            fragmentTransaction.hide(mHomeFragment);
        }

        if (mCategoryFragment != null) {
            fragmentTransaction.hide(mCategoryFragment);
        }

        if (mShoppingCartFragment != null) {
            fragmentTransaction.hide(mShoppingCartFragment);
        }

        if (mMineFragment != null) {
            fragmentTransaction.hide(mMineFragment);
        }
    }

    @Override
    public void currentIndex(int position) {
        Log.d("-----------------", "currentIndex: ----------------------" + position);
        if (position == 2) {
            mBottomNavigationView.setSelectedItemId(R.id.mr_car);
        } else {
            mBottomNavigationView.setSelectedItemId(R.id.mr_category);
        }

        selectedFragment(position);

    }
}